Skip to content

Fix DTMF deduplication to use (timestamp, event code) composite key - #796

Open
cuihang wants to merge 2 commits into
livekit:mainfrom
cuihang:fix/dtmf-dedup-event-code
Open

Fix DTMF deduplication to use (timestamp, event code) composite key#796
cuihang wants to merge 2 commits into
livekit:mainfrom
cuihang:fix/dtmf-dedup-event-code

Conversation

@cuihang

@cuihang cuihang commented Aug 16, 2026

Copy link
Copy Markdown

Problem

The DTMF handler in dtmfHandler (media_port.go) deduplicated incoming DTMF packets using only the RTP timestamp:

if h.Timestamp == p.lastDTMFTimestamp.Load() {
    return nil
}

RFC 4733 requires all packets of a given digit to share identical timestamps, so this correctly filters redundant packets within one digit. However, some SIP devices or carriers reuse the timestamp of the previous digit when sending the next digit. When this happens, the next digit is incorrectly filtered out and never reported.

Fix

Replace the timestamp-only dedup with a (timestamp, event code) composite key:

ev, err := dtmf.Decode(payload)
if err != nil {
    return nil
}
eventID := uint64(h.Timestamp)<<8 | uint64(ev.Code)
if eventID == p.lastDTMFEvent.Load() {
    return nil
}
p.lastDTMFEvent.Store(eventID)
fnc(ev)

Behavior

Scenario Before After
Same digit, redundant packets (same ts + same code) Deduped ✓ Deduped ✓
Different digits sharing timestamp (same ts + different code, e.g. *, 0, 1) Incorrectly dropped Each reported ✓
No dependency on RTP marker bit

Known limitation

If an upstream sends two identical digits (e.g. two 0s) with the exact same timestamp and event code, the (timestamp, event code) key alone cannot distinguish them. Resolving that case requires full DTMF lifecycle state tracking (marker bit, End bit, duration reset, sequence number gaps). That is a more complex enhancement beyond this fix; in the worst case (all fields identical) the receiver cannot distinguish the digits at all.

Changes

  • pkg/sip/media_port.go: Replace lastDTMFTimestamp atomic.Uint32 with lastDTMFEvent atomic.Uint64; update dtmfHandler to decode first, then dedup on composite key.
  • pkg/sip/media_port_test.go: Update field reference; add TestMediaPortDTMFSameTimestamp verifying two different digits (0 and 1) sharing the same RTP timestamp are both reported.

Test plan

  • TestMediaPortDTMF — all 12 existing subtests pass (digits 1/12/123 × loss none/first/last/middle)
  • TestMediaPortDTMFSameTimestamp — new test passes: digits 0 and 1 with same timestamp → both reported as 01

@cuihang
cuihang requested a review from a team as a code owner August 16, 2026 18:12
@CLAassistant

CLAassistant commented Aug 16, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 1 additional finding.

Open in Devin Review

@codecov

codecov Bot commented Aug 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.70%. Comparing base (0460b40) to head (ed941ac).
⚠️ Report is 351 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #796      +/-   ##
==========================================
+ Coverage   65.25%   66.70%   +1.45%     
==========================================
  Files          51       42       -9     
  Lines        6588     8110    +1522     
==========================================
+ Hits         4299     5410    +1111     
- Misses       1915     2211     +296     
- Partials      374      489     +115     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cuihang
cuihang force-pushed the fix/dtmf-dedup-event-code branch from 94cae6a to 135a5fc Compare August 16, 2026 19:04
@cuihang

cuihang commented Aug 22, 2026

Copy link
Copy Markdown
Author

Hi @dennwc , just wanted to check if someone could take a look at this PR. We've hit this DTMF loss issue in production with our SIP carrier. All CI checks pass and the fix is minimal. Happy to make any adjustments if needed.

@dennwc dennwc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Thank you for the fix!

Some SIP devices or carriers may reuse the RTP timestamp of the previous
digit for the next one, causing the current timestamp-only dedup to drop
valid DTMF events.

Change the dedup key from RTP timestamp to a composite (timestamp, event code)
key, so that different digits sharing the same timestamp are still reported
individually, while redundant packets of the same digit are still deduplicated.

Also add a test case to verify the fix.
@cuihang
cuihang force-pushed the fix/dtmf-dedup-event-code branch from cac7746 to 20df4fe Compare August 23, 2026 13:32
The dtmf.Event struct and Encode function signatures changed in the newer
media-sdk version. Update the new test to use dtmf.Write via rtp.Buffer
instead of constructing raw DTMF events directly.
@cuihang

cuihang commented Aug 23, 2026

Copy link
Copy Markdown
Author

Hi @dennwc, just a quick update — I've rebased onto the latest main to resolve the merge conflicts (the dtmf handler had moved to media_pipeline.go). All CI checks are now passing. The core fix logic remains the same as what you reviewed. Could you take another look when you get a chance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants